package cn.lau.demo.componet;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

	private final static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@ExceptionHandler(AuthorizationException.class)
	public String handlerAuthorizationException(AuthorizationException ae) {
		LOGGER.error(ae.getMessage());
		Map<String , Object> result = new HashMap<>();
		result.put("code", 1);
		result.put("msg", "无权访问！详情：" + ae.getMessage());
		result.put("data", null);
		return JSON.toJSONString(result);
	}

}
